import { HttpException, HttpStatus } from '@nestjs/common';
import { ErrorEnum } from '/@/common/constants/error';

/**
 * 业务错误时可抛出该异常
 */
export class ApiException extends HttpException {
  private errorCode: number;

  /**
   * 业务错误
   */
  constructor(err: ErrorEnum | string) {
    super(`${err}`, HttpStatus.OK);
    let errorCode = '5000';
    const errorCodeEnum = Object.entries(ErrorEnum).find(([_, val]) => val === err);
    if (errorCodeEnum) {
      errorCode = errorCodeEnum[0].replace('CODE_', '');
    }
    this.errorCode = Number(errorCode);
  }

  getErrorCode(): number {
    return this.errorCode;
  }
}